<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/14
 * Time: 15:20
 */
namespace Pay\Controller;
use Admin\Controller\CommonController;
use Pay\Model\PayModel;
class PayController extends CommonController
{
    private $pay_db;
    function _initialize()
    {
        parent::_initialize();
        $this->pay_db= new PayModel();
    }

    function index($page = 1, $rows = 10, $search = array())
    {
        if(IS_POST){
            $limit = ($page - 1) * $rows . "," . $rows;
            $total = $this->pay_db->count();
            $list  = $total ? $this->pay_db->order('updatetime desc')->limit($limit)->select() : array();
            foreach ($list as $key=>$value){
                //格式化时间
                $list[$key]['updatetime'] = date('Y-m-d H:i', $value['updatetime']);
                $list[$key]['createtime'] = date('Y-m-d H:i', $value['createtime']);
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
        }else{
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('list');
        }

    }

    /**
     * 上传图片
     * @param int $id
     */
    function public_uploadHead($id=0){
        $data = upload('/Picture/pay/', $id, 'user');
        if (is_string($data)) {
            $this->ajaxReturn(array('info'=>$data, 'status'=>0));
        }else {
            $this->ajaxReturn(array('info'=>'上传成功', 'url'=>$data['0']['smallUrl'], 'status'=>1));
        }
    }

    function edit($id=0){
        if (IS_POST) {
            $data = I('info');
            $data['updatetime']=NOW_TIME;
            if ($this->pay_db->where(array('id'=>$id))->save($data)){
                $this->success('编辑成功');
            }else {
                $this->error('编辑失败');
            }
        }else {
            $this->info = $this->pay_db->where(array('id'=>I('id')))->find();
            $this->rand = I('rand');
            $this->display('edit');
        }
    }

    function add(){
        if (IS_POST) {
            $data = I('info');
            $result=M('pay')->where(array('type'=>$data['type']))->find();
            if(!empty($result)){
                $this->error('该类型已存在，请选择其他方式');
            }
            if($data['type']==1){
                $data['title']='支付宝';
                $data['account']='';
            }else if($data['type']==2){
                $data['title']='微信';
                $data['account']='';
            }else if($data['type']==3){
                $data['title']='银行卡';
            }

            $data['createtime'] = SYS_TIME;
            $data['updatetime']=SYS_TIME;
            if ($this->pay_db->add($data)) {
                $this->success('添加成功');
            }else {
                $this->error('添加失败');
            }
        }else {
            $this->display('add');
        }
    }

    function bank($page = 1, $rows = 10, $search = array())
    {
        if(IS_POST){
            $limit = ($page - 1) * $rows . "," . $rows;
            $total = M('card')->count();
            $list  = $total ?  M('card')->order('create_time desc')->limit($limit)->select() : array();
            foreach ($list as $key=>$value){
                //格式化时间
                $list[$key]['create_time'] = date('Y-m-d H:i', $value['create_time']);
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
        }else{
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('bank/list');
        }
    }

    function bankDelete($id){

        $return=$this->pay_db->bankDelete($id);
        if($return['code']){
            $this->error($return['msg']);
        }else{
            $this->success($return['msg']);
        }
    }

    function bankAdd(){
        if (IS_POST) {
            $data = I('info');
            $result=M('card')->where(array('name'=>$data['name']))->find();
            if(!empty($result)){
                $this->error('该银行已存在，请选择其他方式');
            }
            $data2['name']=$data['name'];
            $data2['create_time'] = SYS_TIME;

            if (M('card')->add($data2)) {
                $this->success('添加成功');
            }else {
                $this->error('添加失败');
            }
        }else {
            $this->display('bank/add');
        }
    }


}

